package com.bawei.shopping2

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.text.TextUtils
import android.widget.RemoteViews
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.shopping2.databinding.ActivityDetailBinding
import com.bawei.shopping2.utils.ArouterPath
import com.bawei.shopping2.viewmodel.DetailViewModel
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = ArouterPath.ROUTE_DETAIL_PATH)
class DetailActivity : AppCompatActivity() {
    @Autowired
    @JvmField //id不能是私有的 添加此注解id变成public
    var id:Int = 0
    private lateinit var activityDetailBinding: ActivityDetailBinding
    private var job = Job()
    private val detailViewModel:DetailViewModel by  viewModels()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activityDetailBinding = ActivityDetailBinding.inflate(layoutInflater)
        setContentView(activityDetailBinding.root)
        ARouter.getInstance().inject(this)//注入
        CoroutineScope(job).launch(Dispatchers.Main) {
            var apiResponse = detailViewModel.goodsDetail(id)
            if(apiResponse.code == 200){
                activityDetailBinding.goods = apiResponse.data
            }
        }
        //添加购物车:判断是否登陆过：
        activityDetailBinding.tvAddCar.setOnClickListener{

                if(!TextUtils.isEmpty(SPUtils.getInstance().getString("token"))){
                    var map = mutableMapOf<String,String>()
                    map["count"] = "1"
                    map["goods_id"] = "$id"
                    var requestBody = map.toBody()
                    CoroutineScope(job).launch(Dispatchers.Main) {
                       var apiResponse =  detailViewModel.addCar(requestBody)
                        if(apiResponse.code == 200){
                            ToastUtils.showLong("添加成功")
                            //发送通知，点击通知跳转到购物车页面
                            sendNotification()


                        }else{
                            ToastUtils.showLong("添加失败")
                        }
                    }
                }else{
                    ToastUtils.showLong("你没登陆过")
                }




        }
        //查看购物车：跳转到购物车页面
        activityDetailBinding.tvShowCar.setOnClickListener{
            ARouter.getInstance().build(ArouterPath.ROUTE_CAR_PATH).navigation()
        }


    }
    //发送通知：安卓8.0以后使用通道发送通知
    private fun sendNotification() {
        //0：通知管理
        val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        //1：判断手机版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //8。0
            val mChannel = NotificationChannel("11111", "通道的名称", NotificationManager.IMPORTANCE_DEFAULT)
            mChannel.description = "通道的描述"
            notificationManager.createNotificationChannel(mChannel)
        }

        //2：通知对象
        //自定义view
        val remoteViews = RemoteViews(packageName, R.layout.notification)
        //点击通知跳转到其他页面
        var pendingIntent: PendingIntent? = null
        pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { //11.0
            PendingIntent.getActivity(
                this,
                101,
                Intent(this, CarActivity::class.java),
                PendingIntent.FLAG_IMMUTABLE
            )
        } else {
            PendingIntent.getActivity(
                this,
                101,
                Intent(this, CarActivity::class.java),
                PendingIntent.FLAG_UPDATE_CURRENT
            )
        }
        var notification: Notification? = null
        notification = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //8。0
            Notification.Builder(this, "11111")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("我是标题")
                .setContentText("我是内容")
                .setContentIntent(pendingIntent) //点击跳转
                .setContent(remoteViews) //设置自定义布局
                .build()
        } else {
            Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("我是标题")
                .setContentText("我是内容")
                .setContentIntent(pendingIntent) //点击跳转
                .setContent(remoteViews) //设置自定义布局
                .build()
        }
        //3：发送通知
        notificationManager.notify(1, notification)

    }
}